<?php

/**
 * @author Lancer He <lancer.he@gmail.com>
 * @copyright 2011
 */

!defined('DEX') && die('Access denied');

Final Class Dex_Front {

    private $router;
    private $config;

    function __construct() {

        $this->router = &loadClass('Router');
        $this->event  = &loadClass('Event');
        //$this->event->log($this->router);
        $this->init();

    }

    private function init() {

        $controlFile = $this->router->getFile();
        $control     = $this->router->getClass();

        //Check controller file exists;
        if ( ! file_exists($controlFile) ) {
            $this->event->error('Controller file is not found: ' . $control);
        }
        require($controlFile);

        //Check controller class exists;
        if ( ! class_exists($control) ) {
            $this->event->error('Controller is not defined: '. $control);
        }
        $this->instance = new $control;
    }

    function run() {

        $action      = $this->router->getMethod();

        if ( ! method_exists($this->instance, $action) ) {
            $this->event->error('Method is not found: ' . $action);
        }

        $this->instance->$action();
    }

}